package shuai6.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

public class UpAction extends BaseAction {
	private String title;
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	
	public String execute(){
		String uploadPath=getSavePath()+"\\"+getUploadFileName();
		doUpload(upload, uploadPath);
		System.out.println(uploadFileName);
		
		return SUCCESS;
	}
	
	/**
	 * 上传文件
	 * @param file 文件
	 * @param uploadFilePath 路径
	 */
	public void doUpload(File file,String uploadFilePath){
		try {
			FileOutputStream fos=new FileOutputStream(uploadFilePath);
			FileInputStream fis=new FileInputStream(file);
			byte[] buffer=new byte[1024];
			int len=0;
			//循环读写
			while((len=fis.read(buffer))!=-1){
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {		
			e.printStackTrace();
		}
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	
}
